Skip to content

[OCONF-166] coconut conf import command#71

Merged
graduta merged 18 commits into
masterfrom
feature/OCONF-166-coconut-conf-import-command
Oct 3, 2019
Merged

[OCONF-166] coconut conf import command#71
graduta merged 18 commits into
masterfrom
feature/OCONF-166-coconut-conf-import-command

Conversation

@graduta
Copy link
Copy Markdown
Member

@graduta graduta commented Oct 1, 2019

No description provided.

@graduta graduta requested review from kostorr and teo October 1, 2019 08:10
@graduta graduta self-assigned this Oct 1, 2019
@teo
Copy link
Copy Markdown
Member

teo commented Oct 1, 2019

Fixes please:

import command

  • s/Extension of the file should be/Supported file extensions. To force a specific configuration format, see flag --format/-f/
  • s/Please check through the already existing components:/Available components in configuration database:/
  • In all simple lists (starting with -), make sure that there's a single space between - and the first letter of the list item.
  • s/If you wish to add a new component, please use flag -n/-new-component to create a new component/To create a new component, see flag --new-component/-n/
  • s/A new component has been created/New component created/ s/A new entry has been created/New entry created/ s/The following configuration key has been added/Configuration key created/
  • s/Component read-teo-test already exists, thus flag -n/-new-component cannot be used/Invalid use of flag --new-component/-n: component red(read-teo-test) already exists/
  • s/Component read-teo-test has been updated//
  • s/Entry myEntry2 has been updated/Entry updated: myEntry2/

show command

  • s/The configuration show command requests by default the latest
    configuration for the specified component and entry. It can request exact
    time configuration by specifying wanted timestamp as flag/The configuration show command returns the most recent configuration revision for the specified component and entry. It can also return a specific revision, requested with the --timestamp/-t flag./

Suggestions:

  • Look into empty lines at the end of output and remove them.

Comment thread coconut/cmd/configuration_import.go Outdated
coconut conf import <component/entry> <file_path> --format=json
coconut conf import <component/entry> <file_path>.json
`,
Short: "Import a configuration file for the component and entry specified",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import a configuration file for the specified component and entry

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread coconut/cmd/configuration_import.go Outdated
Use: "import <component> <entry> <file_path>",
Aliases: []string{"i", "imp"},
Example: `coconut conf import <component> <entry> <file_path>
coconut conf import <component/entry> <file_path>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: I'd put <component>/<entry> instead of <component/entry> as component and entry are the variables here, which we compose in one way or another.

Comment thread coconut/cmd/configuration_import.go Outdated
coconut conf import <component/entry> <file_path>.json
`,
Short: "Import a configuration file for the component and entry specified",
Long: `The configuration import command will generate a timestamp and save
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/will generate/generates/

Comment thread coconut/cmd/configuration_import.go Outdated
`,
Short: "Import a configuration file for the component and entry specified",
Long: `The configuration import command will generate a timestamp and save
the configuration file under the component/entry/timestamp path in Consul. Default
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... save the configuration file to Consul under the path <component>/<entry>/<timestamp>.

Comment thread coconut/cmd/configuration_import.go Outdated
Short: "Import a configuration file for the component and entry specified",
Long: `The configuration import command will generate a timestamp and save
the configuration file under the component/entry/timestamp path in Consul. Default
accepted file extensions are JSON, YAML, TOML, INI`,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Default accepted file extensions are/Supported configuration file types are JSON, YAML, TOML and INI, and their file extensions are recognized automatically./

Comment thread coconut/cmd/configuration_import.go Outdated
func init() {
configurationCmd.AddCommand(configurationImportCmd)
configurationImportCmd.Flags().BoolP("new-component", "n", false, "used to add a new component")
configurationImportCmd.Flags().StringP("format", "f", "", "used to specify a different extension file than default (JSON, YAML, INI, TOML)")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

force a specific configuration file type, overriding any file extension

Comment thread coconut/cmd/configuration_import.go Outdated

func init() {
configurationCmd.AddCommand(configurationImportCmd)
configurationImportCmd.Flags().BoolP("new-component", "n", false, "used to add a new component")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a new configuration component while importing entry

Comment thread coconut/configuration/configuration.go Outdated
for key, _ := range components {
componentMsg += "\n-" + key
}
return errors.New(fmt.Sprintf("Component `" + component + "` does not exist. " +
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use backticks here, either color or nothing. The backticks can potentially make sense in Cobra command doc strings because those are exported as Markdown, but not in error messages.


func isFileExtensionValid(extension string) bool{
extension = strings.ToUpper(extension)
return extension == "JSON" || extension == "YAML" || extension == "INI" || extension == "TOML"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also allow .yml just in case. Visible strings should not be changed, just the logic to approve the file extension.

Comment thread coconut/configuration/configuration.go Outdated
err = errors.New(fmt.Sprintf("Requested component name cannot contain character `/` or `@`"))
return err, invalidArgs
if !isInputSingleValidWord(args[0]) {
return errors.New(fmt.Sprintf(invalidArgsErrMsg)), invalidArgs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may simply be errors.New(invalidArgsErrMsg). https://golang.org/pkg/errors/#New
fmt.Sprintf() is not necessary if you don't have some fancy arguments you want to handle.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not know. Thank you 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also fmt.Errorf that combines the two for when you do need to handle fancy arguments ;)

Comment thread coconut/configuration/configuration.go Outdated
componentsSet[componentsFullName] = componentTimestamp
}
entryExists := false
if useNewComponent {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this clause necessary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not anymore. I negated it


var maxTimeStamp uint64
for _, key := range keys {
componentTimestamp, err := strconv.ParseUint(strings.TrimPrefix(key, keyPrefix + "/"), 10, 64)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine this is done on purpose here, but this leads to variable shadowing, which could in turn lead to errors. A very good explanation can be found here.

Copy link
Copy Markdown
Member Author

@graduta graduta Oct 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I am not interested in stopping the whole command if one of the timestamps is bad. Thus, I am adding it to the list of elements only if it is a valid one. So even if the timestamp is not correct, it will be ignored.

The error that I am interested here is the one with emptyData

Comment on lines +36 to +38
coconut conf import <component> <entry> <file_path> --new-component
coconut conf import <component> <entry> <file_path> --format=json
coconut conf import <component> <entry> <file_path>.json
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to be missing the <component>/<entry> bits.

Copy link
Copy Markdown
Member Author

@graduta graduta Oct 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not missing as the requirement was to have exactly 3 arguments:

coconut conf import <component name> <entry name> <file path> [options]

Would you like that option added?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so coconut conf import <component/entry> <file_path>.json never worked even when it was documented?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. It was a mistake when documentation was added and on second look of the requirements it was then removed.

If we wish to support that we can added so that we are in line with the
coconut conf show command

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be splendid, and also the final thing before we merge.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature implementation looks good, could you please just add back the relevant documentation lines for the <component>/<entry> syntax? Once this is done we can merge.

Comment thread coconut/configuration/configuration.go Outdated
nonZero = iota
invalidArgs = iota // Provided args by the user are invalid
invalidArgsErrMsg = "Component and Entry names cannot contain `/ or `@`"
invalidArgsErrMsg = "component and Entry names cannot contain `/ or `@`"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Entry/entry/

Comment thread coconut/configuration/configuration.go Outdated
}
fullKey := red(component) + "/" + blue(entry) + "@" + strconv.FormatInt(timestamp, 10)
userMsg += "The following configuration key has been added: " + fullKey
userMsg += "Configuration created: " + fullKey
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we discussed this at length trying to find the best wording and we settled on this, but after additional thought I realized that:

  1. this string is the only one that's always printed (the ones for new entry and component aren't)
  2. this string is the last (and sometimes only) line of output in response to a coconut conf import command.

Therefore I propose the following:
userMsg += "Configuration imported: " + fullKey

@graduta graduta merged commit e76aacd into master Oct 3, 2019
@graduta graduta deleted the feature/OCONF-166-coconut-conf-import-command branch October 3, 2019 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants